<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Product Information Management</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom CSS -->
    <link th:href="@{/css/custom.css}" rel="stylesheet">
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

<!-- Navigation Bar -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">My App</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="/page/home">Home</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/page/orders">Orders</a>
            </li>
            <li class="nav-item active">
                <a class="nav-link" href="/page/products">Product  <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/page/members">Member </a>
            </li>
        </ul>
    </div>
</nav>

<!-- Sidebar -->
<div class="container-fluid">
    <div class="row">
        <nav class="col-md-2 d-none d-md-block bg-light sidebar custom-sidebar">
            <div class="sidebar-sticky">
                <ul class="nav flex-column custom-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/page/home">
                            <span data-feather="home"></span>
                            Dashboard
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/page/orders">
                            <span data-feather="file"></span>
                            Orders
                        </a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="/page/products">
                            <span data-feather="shopping-cart"></span>
                            Product
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/page/members">
                            <span data-feather="users"></span>
                            Member
                        </a>
                    </li>
                </ul>
            </div>
        </nav>

        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
            <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                <h1 class="h2">Product Information Management</h1>
            </div>
            <table class="table table-striped" id="productsTable">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Category</th>
                    <th>Actions</th>
                </tr>
                </thead>
                <tbody>
                <!-- Products will be loaded here via AJAX -->
                </tbody>
            </table>
        </main>
    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js"></script>
<script>
    $(document).ready(function() {
        function loadProducts() {
            $.ajax({
                url: '/order/product/page',
                method: 'GET',
                success: function(response) {
                    var data = response.data; // Extract data from response
                    var tbody = $('#productsTable tbody');
                    tbody.empty(); // Clear existing rows
                    data.forEach(function(product) {
                        var row = `
                        <tr>
                            <td>${product.id}</td>
                            <td>${product.name}</td>
                            <td>${product.price}</td>
                            <td>${product.category}</td>
                            <td>
                                <button class="btn btn-sm btn-primary edit-product" data-id="${product.id}">Edit</button>
                                <button class="btn btn-sm btn-danger delete-product" data-id="${product.id}">Delete</button>
                            </td>
                        </tr>
                    `;
                        tbody.append(row);
                    });
                },
                error: function(error) {
                    console.error('Error loading products:', error);
                }
            });
        }

        // Load products on page load
        loadProducts();

        $(document).on('click', '.edit-product', function() {
            var productId = $(this).data('id');
            // Open a modal for editing the product
            $('#createProductModal').modal('show');
            // Load product details into the modal
            $.ajax({
                url: '/product/getProductById/' + productId,
                method: 'GET',
                success: function(data) {
                    $('#productName').val(data.name);
                    $('#productPrice').val(data.price);
                    $('#productCategory').val(data.category);
                    $('#submitProductButton').text('Update').attr('data-id', productId);
                },
                error: function(error) {
                    console.error('Error loading product details:', error);
                }
            });
        });

        // Add a modal for product creation/editing
        $('#createProductModal').on('hidden.bs.modal', function () {
            $('#submitProductButton').text('Create').removeAttr('data-id');
        });

        $('#submitProductButton').click(function() {
            var productId = $(this).data('id');
            var productName = $('#productName').val();
            var productPrice = $('#productPrice').val();
            var productCategory = $('#productCategory').val();

            var url = productId ? '/product/updateProduct/' + productId : '/product/createProduct';
            var method = productId ? 'PUT' : 'POST';

            $.ajax({
                url: url,
                method: method,
                data: {
                    name: productName,
                    price: productPrice,
                    category: productCategory
                },
                success: function(response) {
                    console.log('Product saved successfully:', response.data); // Extract data from response
                    $('#createProductModal').modal('hide');
                    loadProducts(); // Reload products to reflect the changes
                },
                error: function(error) {
                    console.error('Error saving product:', error);
                }
            });
        });

        $(document).on('click', '.delete-product', function() {
            var productId = $(this).data('id');
            // Handle delete product logic here
            console.log('Delete product:', productId);
        });

    });
</script>

</body>
</html>
